import {useValues} from "../../../../components/filter/composables/useValues";
import {Value} from "../../../../components/filter/utils/types";

export enum Comparators {
    EQUALS = "=",
    NOT_EQUALS = "!=",
    GREATER_THAN = ">",
    LESS_THAN = "<",
    GREATER_THAN_OR_EQUAL_TO = ">=",
    LESS_THAN_OR_EQUAL_TO = "<=",
    STARTS_WITH = "^=",
    ENDS_WITH = "$=",
    CONTAINS = "*=",
    REGEX = "~=",
    PREFIX = "^.=",
}

export function keyOfComparator(comparator: Comparators): keyof typeof Comparators {
    return Object.entries(Comparators).find(([_, value]) => value === comparator)![0] as keyof typeof Comparators;
}

export function getComparator(comparatorKey: keyof typeof Comparators | "IN" | "NOT_IN"): Comparators {
    switch(comparatorKey) {
        case "IN": {
            return Comparators.EQUALS;
        }
        case "NOT_IN": {
            return Comparators.NOT_EQUALS;
        }
        default: {
            return Comparators[comparatorKey as keyof typeof Comparators];
        }
    }
}

export class Completion {
    private readonly _label: string;
    private readonly _value: string;

    get label(): string {
        return this._label;
    }

    get value(): string {
        return this._value;
    }

    constructor(label: string, value: string) {
        this._label = label;
        this._value = value;
    }
}

export const PICK_DATE_VALUE = "PICK_DATE";

export type ValueCompletions = Value[] | typeof PICK_DATE_VALUE;

export type Fetcher = (hardcodedValues: ReturnType<typeof useValues>["VALUES"]) => Promise<ValueCompletions>;

export class FilterKeyCompletions {
    private readonly _comparators: Comparators[];
    private readonly _valuesFetcher: Fetcher;
    private readonly _allowMultipleValues: boolean;
    private readonly _forbiddenConcurrentKeys: string[];

    constructor(
        comparators: Comparators[],
        valuesFetcher: Fetcher = async () => [],
        allowMultipleValues?: boolean,
        forbiddenConcurrentKeys: string[] = []
    ) {
        this._comparators = comparators;
        this._valuesFetcher = valuesFetcher;
        this._allowMultipleValues = allowMultipleValues ?? false;
        this._forbiddenConcurrentKeys = forbiddenConcurrentKeys;
    }

    get comparators(): Comparators[] {
        return this._comparators;
    }

    get valuesFetcher(): Fetcher {
        return this._valuesFetcher;
    }

    get allowMultipleValues(): boolean {
        return this._allowMultipleValues;
    }

    get forbiddenConcurrentKeys(): string[] {
        return this._forbiddenConcurrentKeys;
    }
}
